package cn.bocweb.spinning.view.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ConfigTable;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.ExamDataBean;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.OptionBean;
import cn.bocweb.spinning.model.Bean.PointBean;
import cn.bocweb.spinning.model.Bean.RightBean;
import cn.bocweb.spinning.model.Bean.SecondDownloadBean;
import cn.bocweb.spinning.model.Bean.SecondEnterBean;
import cn.bocweb.spinning.model.Bean.SecondMatch;
import cn.bocweb.spinning.model.Bean.SecondTimeBean;
import cn.bocweb.spinning.model.Bean.StartExamBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.AES256Utils;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyDbHelper;
import cn.bocweb.spinning.view.common.SecondTestService;
import de.greenrobot.event.EventBus;

import static android.R.attr.data;

public class ReadyTestActivity extends BaseActivity implements View.OnClickListener {

    String userid, libtype = null, title, qtitle;
    UserPresenter presenter;
    UserBean.DataEntity user;
    boolean isFirstMatch = false, isSecondTest = false;
    String endTime;
    Intent intent;
    //    DBHelper dbHelper = new DBHelper(this);
    MyDbHelper myDbHelper = new MyDbHelper();
    boolean isDownload = false;
    List<StartExamBean.DataEntity> list = new ArrayList<>();
    String order;
    int snum, jnum, dnum;
    DbUtils db;
    String title1 = null;

    String second_test_id;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.ready_exam_icon)
    ImageView readyExamIcon;
    @Bind(R.id.ready_exam_nickname)
    TextView readyExamNickname;
    @Bind(R.id.ready_exam_warehouse_title)
    TextView readyExamWarehouseTitle;
    @Bind(R.id.ready_exam_address)
    TextView readyExamAddress;
    @Bind(R.id.ready_exam_warehouse_num)
    TextView readyExamWarehouseNum;
    @Bind(R.id.ready_exam_time_limit)
    TextView readyExamTimeLimit;
    @Bind(R.id.ready_exam_login_account)
    EditText readyExamLoginAccount;
    @Bind(R.id.ready_exam_login_pwd)
    EditText readyExamLoginPwd;
    @Bind(R.id.ready_exam_login_submit)
    Button readyExamLoginSubmit;
    @Bind(R.id.ready_exam_login_register)
    TextView readyExamLoginRegister;
    @Bind(R.id.ready_exam_login_forget_pwd)
    TextView readyExamLoginForgetPwd;
    @Bind(R.id.ready_exam_login_area)
    LinearLayout readyExamLoginArea;
    @Bind(R.id.ready_exam_start)
    Button readyExamStart;
    @Bind(R.id.ready_exam_total_score)
    TextView readyExamTotalScore;
    @Bind(R.id.ready_exam_request_score)
    TextView readyExamRequestScore;
    @Bind(R.id.ready_exam_edit)
    TextView readyExamEdit;
    @Bind(R.id.lib_layout)
    LinearLayout mLibLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ready_test);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        presenter = new UserPresenterImp();
        dialog.show();
        db = myDbHelper.getDbInstance(this);
//        presenter.sendPoint();
//        initEvent();
        initIntent();
        initClickBtn();
        initBean();
    }

    private void initBean() {
        if (isSecondTest) {
            ConfigTable config = new ConfigTable();
            try {
                config = db.findFirst(Selector.from(ConfigTable.class).where("libtype", "=", libtype));
            } catch (DbException e) {
                e.printStackTrace();
            }

            snum = Integer.parseInt(config.getSnum());
            dnum = Integer.parseInt(config.getDnum());
            jnum = Integer.parseInt(config.getJnum());
            int spoint = Integer.parseInt(config.getSpoint());
            int dpoint = Integer.parseInt(config.getDpoint());
            int jpoint = Integer.parseInt(config.getJpoint());
            String timelimit = config.getTimelimit();
            String isScroll = config.getIshuadong();
            String isdatika = config.getIsdatika();
            String ischange = config.getIschange();
            String end = config.getEnd();
            order = config.getMyorder();
            Log.e("====end=", end + "timelimit=" + timelimit);
            readyExamWarehouseNum.setText(String.valueOf(snum + dnum + jnum));
            readyExamTimeLimit.setText(timelimit);

            readyExamRequestScore.setText(config.getRequire());
            readyExamTotalScore.setText(String.valueOf(snum * spoint + dnum * dpoint + jpoint * jnum));
            readyExamWarehouseTitle.setText(config.getTitle());
            intent = new Intent(this, TestActivity.class);
            intent.putExtra(Constant.START_EXAM_TIMELIMIT, timelimit);
            intent.putExtra(Constant.TO_TEST_SELPOINT, spoint);
            intent.putExtra(Constant.TO_TEST_JUDGEPOINT, jpoint);
            intent.putExtra(Constant.TO_TEST_DUOXUANPOINT, dpoint);
            intent.putExtra(Constant.TO_TEST_IS_CHANGE, ischange);
            intent.putExtra(Constant.TO_TEST_DATIKA, isdatika);
            intent.putExtra(Constant.TO_TEST_SCROLLABLE, isScroll);
            intent.putExtra(Constant.START_EXAM_END_TIME, end);
//                }
        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (isSecondTest) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    List<SecondMatch> listMatch1 = new ArrayList<SecondMatch>();
                    List<SecondMatch> listMatch2 = new ArrayList<SecondMatch>();
                    List<SecondMatch> listMatch3 = new ArrayList<SecondMatch>();
                    List<SecondMatch> listMatch4 = new ArrayList<SecondMatch>();

                    try {
                        listMatch1 = db.findAll(Selector.from(SecondMatch.class).where("libtype", "=", libtype)
                                .and(WhereBuilder.b("ttype", "=", 1))
                                .orderBy("random()")
                                .limit(snum));

                        listMatch2 = db.findAll(Selector.from(SecondMatch.class).where("libtype", "=", libtype)
                                .and(WhereBuilder.b("ttype", "=", 2))
                                .orderBy("random()")
                                .limit(jnum));
                        listMatch3 = db.findAll(Selector.from(SecondMatch.class).where("libtype", "=", libtype)
                                .and(WhereBuilder.b("ttype", "=", 3))
                                .orderBy("random()")
                                .limit(dnum));
                        listMatch4 = db.findAll(Selector.from(SecondMatch.class).where("libtype", "=", libtype)
                                .orderBy("random()")
                                .limit(snum + jnum + dnum));
                    } catch (DbException e) {
                        e.printStackTrace();
                    }


                    switch (order) {

                        case "123": {
                            initQuery(listMatch1);
                            initQuery(listMatch2);
                            initQuery(listMatch3);
                            break;
                        }
                        case "132": {
                            initQuery(listMatch1);
                            initQuery(listMatch3);
                            initQuery(listMatch2);
                            break;
                        }
                        case "213": {
                            initQuery(listMatch2);
                            initQuery(listMatch1);
                            initQuery(listMatch3);
                            break;
                        }
                        case "231": {
                            initQuery(listMatch2);
                            initQuery(listMatch3);
                            initQuery(listMatch1);
                            break;
                        }
                        case "312": {
                            initQuery(listMatch3);
                            initQuery(listMatch1);
                            initQuery(listMatch2);
                            break;
                        }
                        case "321": {
                            initQuery(listMatch1);
                            initQuery(listMatch2);
                            initQuery(listMatch3);
                            break;
                        }
                        default: {
                            initQuery(listMatch4);
                            break;
                        }
                    }
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    setMyIntent();
                }
            }).start();
        } else {
            presenter.startExam(userid, libtype);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constant.SET_WARE_TO_MAIN) {
            LibListBean.DataEntity dataEntity = new LibListBean.DataEntity();
            dataEntity.readLibListIntoLocal(this);
            libtype = dataEntity.getId();
            readyExamWarehouseTitle.setText(dataEntity.getTitle());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);

        }
    }


    protected void initQuery(List<SecondMatch> matchList) {

        Collections.shuffle(matchList);
        for (int i = 0; i < matchList.size(); i++) {

            String id = null, explain = null, title = null, option = null, right = null, ttype = null;
            StartExamBean.DataEntity databean = new StartExamBean.DataEntity();
            try {

//                        String libtype = AES256Utils.getInstance().decryptSecure(config.getlibtype")));
                id = matchList.get(i).getQuestionid();
                ttype = matchList.get(i).getTtype();

                explain = AES256Utils.getInstance().decryptSecure(matchList.get(i).getExplain());
                title = AES256Utils.getInstance().decryptSecure(matchList.get(i).getTitle());
                option = matchList.get(i).getOption();

                right = matchList.get(i).getRight();

            } catch (Exception e) {
                e.printStackTrace();
            }

            StringBuffer rightbuff = new StringBuffer();
            rightbuff.append("{\"right\":");
            rightbuff.append(right);
            rightbuff.append("}");
            Gson gson = new Gson();
            List<String> rightj = new ArrayList<String>();
            RightBean rights = new RightBean();
            rights = gson.fromJson(rightbuff.toString(), RightBean.class);
            for (int i1 = 0; i1 < rights.getRight().length; i1++) {
                rightj.add(rights.getRight()[i1]);
            }


            if (Integer.parseInt(ttype) == 2) {

                databean.setOption(null);
            } else {
                if (option != null && !option.isEmpty()) {
                    StringBuffer optionbuff = new StringBuffer();
                    optionbuff.append("{\"option\":");
                    optionbuff.append(option);
                    optionbuff.append("}");
                    Gson gson3 = new Gson();
                    List<String> optionj = new ArrayList<String>();
                    OptionBean options = new OptionBean();
                    String a = optionbuff.toString();
                    options = gson3.fromJson(optionbuff.toString(), OptionBean.class);
                    optionj = options.getOption();
                    databean.setOption(optionj);
                }
            }


            databean.setTid(id);
            databean.setTitle(title);
            databean.setExplain(explain);
            databean.setTtype(ttype);
            databean.setRight(rightj);
//
            list.add(databean);

//                Log.e("====","rights"+list.get(0).getTitle());
        }

    }


    private void initIntent() {
        Intent intent = getIntent();

        if (intent.getStringExtra(Constant.TO_READY_FIRST_TEST_ID) != null) {
            libtype = intent.getStringExtra(Constant.TO_READY_FIRST_TEST_ID);
        }
        if (libtype == null || libtype.isEmpty()) {

            initToolbar("开始考试");
            //轮播图进考一考模版
            if (intent.getStringExtra(Constant.TO_READY_TEST_NORMAL) != null) {
                isFirstMatch = true;
                libtype = intent.getStringExtra(Constant.TO_READY_TEST_NORMAL);
                title1 = intent.getStringExtra(Constant.TO_REDAY_TEST_TITLE);
                readyExamWarehouseTitle.setText(title1);
                initToolbar(title1);
                user = new UserBean.DataEntity();
                user.readInfoFromLocal(this);
                userid = user.getUserid();
//            Log.e("=====userid","="+userid);
                readyExamStart.setVisibility(View.VISIBLE);
                readyExamLoginArea.setVisibility(View.GONE);
                String icon = user.getUthumb();
                String nickname = user.getNickname();
                String address = user.getAddress();

                Glide.with(this)
                        .load(icon)
                        .transform(new GlideCircle(this))
                        .placeholder(R.mipmap.drawer_icon_unlogin)
                        .into(readyExamIcon);

                if (nickname == null || nickname.isEmpty()) {
                    readyExamNickname.setText(Constant.NICKNAME_UNLOGIN);
                } else {
                    readyExamNickname.setText(nickname);

                }

                if (address == null || address.isEmpty()) {
                    readyExamAddress.setText(Constant.ADDRESS_UNLOGIN);
                } else {
                    readyExamAddress.setText(address);
                }
            } else {
                initExamInfo(false);
                isFirstMatch = false;
            }

        } else {
            title = intent.getStringExtra(Constant.TO_REDAY_TEST_TITLE);
            qtitle = intent.getStringExtra(Constant.TO_REDAY_TEST_QTITLE);

            initToolbar(title);
            isSecondTest = intent.getBooleanExtra(Constant.TO_SECOND_RULE_IS_SECOND_TEST, false);

            initExamInfo(true);
            isFirstMatch = intent.getBooleanExtra(Constant.TO_TEST_RANK_IF_FIRST, false);
            readyExamWarehouseTitle.setText(qtitle);
            //复赛名称要改
            if (!isSecondTest) {

                mLibLayout.setVisibility(View.VISIBLE);
                readyExamStart.setText("立即考试");
                readyExamEdit.setVisibility(View.GONE);

            } else {
                second_test_id = intent.getStringExtra(Constant.TO_SECOND_RULE_TEST_ID);
                mLibLayout.setVisibility(View.VISIBLE);

//                readyExamWarehouseTitle.setText("梳理复赛");
//                initToolbar("\"金轮-蓝钻杯\"全国梳理知识大奖赛复赛");

//                readyExamWarehouseTitle.setText("梳理决赛");

                readyExamStart.setText("立即考试");
                readyExamWarehouseNum.setText("50");
                readyExamEdit.setVisibility(View.GONE);
                endTime = intent.getStringExtra(Constant.TO_SECOND_RULE_END_TIME);
                readyExamTimeLimit.setText("15");
            }
        }
    }

    public void onEventMainThread(SecondEnterBean bean) {
        if (bean.getReturn_code() == 0) {
            intent.putExtra(Constant.TO_TEST_TEST_ID, bean.getTestid());
            intent.putExtra(Constant.TO_TEST_TEST_TIMELIMIT, bean.getTimelimit());
            intent.putExtra(Constant.TO_TEST_TEST_START_TIME, bean.getStart_time());
            DeviceUtil.Log("secondEnter O = " + bean.getMsg());
            startActivity(intent);
            finish();
        } else {
            readyExamStart.setClickable(true);
            DeviceUtil.Log("secondEnter !0 = " + bean.getMsg());
        }
    }

    private void initClickBtn() {
        readyExamStart.setOnClickListener(this);
        readyExamLoginSubmit.setOnClickListener(this);
        readyExamLoginRegister.setOnClickListener(this);
        readyExamLoginForgetPwd.setOnClickListener(this);
        readyExamEdit.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ready_exam_start: {

                if (intent == null) {
                    DeviceUtil.MyToast(ReadyTestActivity.this, "正在获取考题");
                } else {
                    readyExamStart.setClickable(false);
//                    presenter.sendPoint();
                    if (readyExamWarehouseNum.getText().equals("0")) {
                        DeviceUtil.MyToast(this, "该题库暂无考题，请更换试题类型");
                    } else {
                        if (isSecondTest) {

                            presenter.secondEnter(userid, libtype);
                        } else {
                            startActivity(intent);
                            finish();
                        }

                    }

                }
                break;
            }
            case R.id.ready_exam_login_submit: {
                String phone = readyExamLoginAccount.getText().toString();
                String pwd = readyExamLoginPwd.getText().toString();
                presenter.login(phone, pwd);
                break;
            }
            case R.id.ready_exam_login_register: {
                Intent intent = new Intent(this, RegisterActivity.class);
                intent.putExtra(Constant.TO_REGISTER, Constant.READY_TEST_TO_REGISTER);
                startActivity(intent);
                break;
            }
            case R.id.ready_exam_login_forget_pwd: {
                Intent intent = new Intent(this, ForgetPwdActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.ready_exam_edit: {
                Intent intent = new Intent(this, SetQuesWarehouseActivity.class);
                startActivityForResult(intent, Constant.SET_WARE_TO_MAIN);
                break;
            }
        }
    }

//    public void onEventMainThread(SecondTimeBean bean) {
//        DeviceUtil.Log("bean = " + bean.getMsg());
//        if (bean.getReturn_code() == 0) {
//            SharedPreferences sp = getSharedPreferences(Constant.SECOND_TIME, Context.MODE_PRIVATE);
//            String second_time_local = sp.getString(Constant.STRING_SECOND_TIME, "");
//            DeviceUtil.Log("本地时间戳 = " + second_time_local);
//            DeviceUtil.Log("后台时间戳 = " + bean.getData().getTimeline());
//            DeviceUtil.Log("文件是否存在 = " + new File(getCacheDir().getAbsolutePath() + "/secondTest.txt").exists());
//            if (!second_time_local.equals(bean.getData().getTimeline()) ||
//                    !new File(getCacheDir().getAbsolutePath() + "/secondTest.txt").exists()) {
//                sp.edit().putString(Constant.STRING_SECOND_TIME, bean.getData().getTimeline()).apply();
//                Intent intent = new Intent(this, SecondTestService.class);
//                startService(intent);
//            }
//        }
//    }

    private void initExamInfo(boolean isFirstMatch) {
        user = new UserBean.DataEntity();
        user.readInfoFromLocal(this);

        if (!isFirstMatch) {
            LibListBean.DataEntity liblist = new LibListBean.DataEntity();
            liblist.readLibListIntoLocal(this);
            libtype = liblist.getId();
            readyExamWarehouseTitle.setText(liblist.getTitle());
            readyExamTotalScore.setText(liblist.getTotalScore());
            readyExamRequestScore.setText(liblist.getRequireScore());
            mLibLayout.setVisibility(View.VISIBLE);

        }
//        else {
//            mLibLayout.setVisibility(View.GONE);
//        }

        if (user.getUserid() == null || user.getUserid().isEmpty()) {
            readyExamLoginArea.setVisibility(View.VISIBLE);
            readyExamStart.setVisibility(View.GONE);
            readyExamIcon.setImageResource(R.mipmap.drawer_icon_unlogin);
            readyExamNickname.setText("未登录");
            readyExamAddress.setText("未登录");
        } else {
            userid = user.getUserid();
            readyExamStart.setVisibility(View.VISIBLE);
            readyExamLoginArea.setVisibility(View.GONE);
            String icon = user.getUthumb();
            String nickname = user.getNickname();
            String address = user.getAddress();

            Glide.with(this)
                    .load(icon)
                    .transform(new GlideCircle(this))
                    .placeholder(R.mipmap.drawer_icon_unlogin)
                    .into(readyExamIcon);

            if (nickname == null || nickname.isEmpty()) {
                readyExamNickname.setText(Constant.NICKNAME_UNLOGIN);
            } else {
                readyExamNickname.setText(nickname);
            }

            if (address == null || address.isEmpty()) {
                readyExamAddress.setText(Constant.ADDRESS_UNLOGIN);
            } else {
                readyExamAddress.setText(address);
            }
        }
    }

    private void initToolbar(String title) {
        toolbar.setTitle(title);
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void onEventMainThread(UserBean bean) {
        if (bean.getReturn_code() == 0) {
            user = bean.getData();
            user.writeInfoIntoLocal(this);
            Intent intent = new Intent(this, ReadyTestActivity.class);
            intent.putExtra(Constant.TO_READY_FIRST_TEST_ID, libtype);
            intent.putExtra(Constant.TO_SECOND_RULE_IS_SECOND_TEST, isSecondTest);

            startActivity(intent);
            finish();
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    public void onEventMainThread(StartExamBean bean) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (bean.getReturn_code() == 0) {
            list = bean.getData();
            intent = new Intent(this, TestActivity.class);

//            intent.putExtra(Constant.START_EXAM_TIMELIMIT, bean.getTimelimit());
            setMyIntent(bean);
        } else {
            if (bean.getReturn_code() != -201) {
                DeviceUtil.MyToast(this, bean.getMsg());
            }
            readyExamTimeLimit.setText(bean.getTimelimit());
            readyExamWarehouseNum.setText(String.valueOf(bean.getSum()));
            readyExamTotalScore.setText(bean.getTotalScore());
            readyExamRequestScore.setText(bean.getRequireScore());
        }

        readyExamStart.setClickable(true);
    }

    public void setMyIntent(StartExamBean bean) {
        intent.putExtra(Constant.START_EXAM_LIST, (Serializable) list);
        intent.putExtra(Constant.START_EXAM_LIBID, libtype);
        intent.putExtra(Constant.START_EXAM_ISFIRSTMATCH, isFirstMatch);
        intent.putExtra(Constant.START_EXAM_ISSECONDTEST, isSecondTest);
        if (title1 == null) {
            intent.putExtra(Constant.TO_REDAY_TEST_TITLE, title);
        } else {
            intent.putExtra(Constant.TO_REDAY_TEST_TITLE, title1);
        }

//        if (!isSecondTest) {

        intent.putExtra(Constant.START_EXAM_TESTID, bean.getTestid());
        intent.putExtra(Constant.START_EXAM_USERID, bean.getUserid());

//        }
        if (!TextUtils.isEmpty(endTime)) {
            intent.putExtra(Constant.START_EXAM_END_TIME, endTime);

        } else {
            intent.putExtra(Constant.START_EXAM_TIMELIMIT, bean.getTimelimit());
        }
//改活
        if (bean.getTimelimit() != null) {
            readyExamTimeLimit.setText(bean.getTimelimit());
        } else {
            readyExamTimeLimit.setText("15");
        }
        readyExamWarehouseNum.setText(String.valueOf(bean.getSum()));
        readyExamWarehouseNum.setText(String.valueOf(bean.getSum()));
        readyExamTotalScore.setText(bean.getTotalScore());
        readyExamRequestScore.setText(bean.getRequireScore());
    }

    public void setMyIntent() {
        intent.putExtra(Constant.START_EXAM_LIST, (Serializable) list);
        intent.putExtra(Constant.START_EXAM_LIBID, libtype);

        intent.putExtra(Constant.TO_REDAY_TEST_TITLE, title);

//        try {
//            readyExamWarehouseTitle.setText(AES256Utils.getInstance().decryptSecure(bean.getSecond()));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        intent.putExtra(Constant.START_EXAM_ISFIRSTMATCH, isFirstMatch);
        intent.putExtra(Constant.START_EXAM_ISSECONDTEST, isSecondTest);

//        if (!TextUtils.isEmpty(endTime)) {
//            intent.putExtra(Constant.START_EXAM_END_TIME, endTime);
//        } else {
//            intent.putExtra(Constant.START_EXAM_TIMELIMIT, time);
//        }
//        DeviceUtil.Log("list = " + bean.getData().get(0).getRight());

//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    //要改活
//
//
//
////                    readyExamTimeLimit.setText(bean.getTimelimit());
////                    readyExamRequestScore.setText(bean.getRequireScore());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                if (dialog.isShowing()) {
//                    dialog.dismiss();
//                }
//            }
//        });
    }

    public void onEventMainThread(ErrorBean bean) {
        hideLoading();
        DeviceUtil.MyToast(this, bean.getErrorMsg());
        readyExamStart.setClickable(true);
    }
}
